package com.company.cloud.work.modules.mgr.controller;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

import com.company.cloud.common.auth.annotation.StaffLogc;
import com.company.cloud.common.bean.BaseClientResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import com.company.cloud.work.modules.base.entity.WorkCommentInfo;
import com.company.cloud.work.modules.base.service.WorkCommentInfoService;
import com.company.cloud.work.modules.cache.models.WorkCommentModel;
import com.company.cloud.work.modules.cache.utils.CrmWorkChangeUtils;
import com.company.cloud.work.modules.mgr.controller.req.WorkCommentQuery4PageParam;
import com.company.cloud.work.modules.mgr.controller.req.WorkCommentSaveParam;
import com.company.cloud.work.modules.mgr.controller.req.WorkCommentUpdateParam;
import com.company.cloud.work.modules.mgr.service.WorkCommentMgrService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@Authc
@RestController
@RequestMapping("/mgr/crm/workComment")
@Api(tags = "CRM_Mgr_点评管理")
public class WorkCommentMgrController {

	@Autowired
	private WorkCommentInfoService workCommentInfoService;

	@Autowired
	private WorkCommentMgrService workCommentMgrService;

	@Autowired
	private UserRemoteService userRemoteService;

	@PostMapping("/query")
	@ApiOperation(value = "查询", response = WorkCommentModel.class)
	public BaseResult query(@RequestBody WorkCommentQuery4PageParam params) {
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		IPage<WorkCommentInfo> page = workCommentInfoService.page(new Page<>(params.getPage(), params.getRows()),
				new QueryWrapper<WorkCommentInfo>().eq("rows_state", EnabledEnum.ENABLED.getCode())
						.eq("company_no", userInfo.getCompanyNo())
						.eq(CheckUtils.hasInt(params.getUserId()), "user_id", params.getUserId())
						.eq(StringUtils.hasText(params.getRelBizType()), "rel_biz_type", params.getRelBizType())
						.eq(StringUtils.hasText(params.getRelBizId()), "rel_biz_id", params.getRelBizId()));

		if (page == null || CollectionUtils.isEmpty(page.getRecords())) {
			return BaseResult.ok();
		}
		Set<Integer> userIds=new HashSet<Integer>();
		page.getRecords().forEach(item->{
			if(CheckUtils.hasInt(item.getUserId())) {
				userIds.add(item.getUserId());
			}
		});
		BaseClientResult<Map<Integer, UserSimple>> userMapResult=userRemoteService.getUserMapByIds(userIds);
		Map<Integer, UserSimple> userMap=BaseClientResult.getResultData(userMapResult,new HashMap<Integer, UserSimple>());
		IPage<WorkCommentModel> data = page.convert(new Function<WorkCommentInfo, WorkCommentModel>() {
			@Override
			public WorkCommentModel apply(WorkCommentInfo entity) {
				return CrmWorkChangeUtils.getWorkCommentModel(entity,userMap);
			}
		});
		return BaseResult.ok(data);
	}

	@PostMapping("/add")
	@ApiOperation(value = "新增", response = WorkCommentModel.class)
	@StaffLogc(title = "新增点评", action = "新增点评", actionAdo = "点评管理")
	public BaseResult add(@RequestBody WorkCommentSaveParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		WorkCommentModel data = workCommentMgrService.save(params, userInfo);
		return BaseResult.ok(data);
	}

	@PostMapping("/edit")
	@ApiOperation(value = "修改", response = BaseResult.class)
	@StaffLogc(title = "修改点评", action = "修改点评", actionAdo = "点评管理")
	public BaseResult edit(@RequestBody WorkCommentUpdateParam params) {
		ValidatorUtils.validate(params);
		WorkCommentInfo entity = workCommentInfoService.getById(params.getId());
		if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		entity.setRelBizType(params.getRelBizType());
		entity.setRelBizId(params.getRelBizId());
		entity.setComment(params.getComment());
		entity.setCommentDate(LocalDateTime.now());
		entity.addEditParam(userInfo);
		workCommentInfoService.updateById(entity);

		return BaseResult.ok();
	}

	@GetMapping("/get")
	@ApiOperation(value = "获取", response = WorkCommentModel.class)
	public BaseResult get(@RequestParam("id") Integer id) {
		WorkCommentInfo entity = workCommentInfoService.getById(id);
		if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}

		Set<Integer> userIds = new HashSet<Integer>();
		userIds.add(entity.getUserId());
		BaseClientResult<Map<Integer, UserSimple>> userMapResult = userRemoteService.getUserMapByIds(userIds);
		WorkCommentModel data = CrmWorkChangeUtils.getWorkCommentModel(entity, BaseClientResult.getResultData(userMapResult));
		return BaseResult.ok(data);
	}

	@GetMapping("/del")
	@ApiOperation(value = "删除", response = BaseResult.class)
	@StaffLogc(title = "删除点评", action = "删除点评", actionAdo = "点评管理")
	public BaseResult del(@RequestParam("id") Integer id) {
		WorkCommentInfo entity = workCommentInfoService.getById(id);
		if (entity == null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		entity.setRowsState(EnabledEnum.DISABLED.getCode());
		entity.addEditParam(userInfo);
		workCommentInfoService.updateById(entity);
		return BaseResult.ok();
	}

}
